<?php

	namespace Phptricks\app\models\settings;

/*
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 */
	/**
	 * Description of index
	 *
	 * @author Sammy Guergachi <sguergachi at gmail.com>
	 */
	use Phptricks\system\library\models\models as models;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\core\library\library;

	class settings extends models implements library{

		private static $_instance = null;

		public static function &getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\app\models\settings\settings();
			}
			return self::$_instance;
		}

		public function getsettings($where = '', $array_decode = array()){
			$setting_data = $this->Query('SELECT * FROM settings', $where);
			$array_return = array();
			if (\count($setting_data) > 0) {
				foreach ($setting_data as $key => $value) {
					if (\count($array_decode) > 0) {
						if (\in_array($value['setting_name'], $array_decode)) {
							if (!empty($value['setting_value'])) {
								$value['setting_value'] = $this->custom_fields->decode($value['setting_value']);
							}
						} else {
							throw new Exc('some fields : ' . \implode(', ', $array_decode) . ' is unknown');
						}
					}
					$array_return[$value['setting_name']] = $value['setting_value'];
				}
			}
			return $array_return;
		}

		public function addsettings($array_data = array()){
			return $this->insert('settings', $array_data);
		}

		public function updatesettings($array_data = array()){
			try {
				foreach ($array_data as $field_key => $field_val) {
					if (!empty($field_key)) {
						$updata = $this->db->prepare("UPDATE settings SET setting_value = :settingvalue WHERE setting_name = :settingname");
						$updata->bindParam(':settingname', $field_key);
						$updata->bindParam(':settingvalue', $field_val);
						$updata->execute();
					}
				}
			} catch (\PDOException $e) {
				throw new Exc($e->getMessage());
			}
			return true;
		}

		public function deletesettings($array_data){
			return $this->delete('settings', $array_data);
		}

	}
	